#pragma once

#include <iostream>
#include <string>
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <time.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

//日志等级
#define Info    0
#define Debug   1
#define Warning 2
#define Error   3
#define Fatal   4

#define SIZE 1024

//打印去向
#define Screen 1
#define File 2
#define MulFile 3

#define LogFile "log.txt"

enum
{
    PrintFile_Open_Err = 1
};

class Log
{
public:
    Log()
    : _printmethod(Screen)
    , _path("./log/")
    {}

    //将等级转字符串
    std::string LevelToString(int level)
    {
        switch(level)
        {
        case Info:
            return "Info";
        case Debug:
            return "Debug";
        case Warning:
            return "Warning";
        case Error:
            return "Error";
        case Fatal:
            return "Fatal";
        default:
            return "None";
        }
    }

    //改变打印方式
    void Enable(int method)
    {
        _printmethod = method;
    }

    //打印到文件
    void PrintFile(const std::string& filename, const std::string& logtxt)
    {
        //打开文件
        std::string newfilename = _path + filename;
        int fd = open(newfilename.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0666);
        if(fd < 0)
        {
            perror("open");
            exit(PrintFile_Open_Err);
        }

        //写入
        write(fd, logtxt.c_str(), logtxt.size());

        //关闭文件
        close(fd);
    }

    //打印到多个文件
    void PrintMulFile(int level, const std::string& logtxt)
    {
        std::string filename = LogFile;
        filename += ".";
        filename += LevelToString(level);
        PrintFile(filename, logtxt);
    }

    //打印日志
    void PrintLog(int level, const std::string& logtxt)
    {
        switch(_printmethod) 
        {
        case Screen: //打印到屏幕
            std::cout << logtxt << std::endl;
            break;
        case File: //打印到文件
            PrintFile(LogFile, logtxt);
            break;
        case MulFile: //打印到多个文件
            PrintMulFile(level, logtxt);
            break;
        default:
            break;
        }
    }

    void operator()(int level, const char* format, ...)
    {
        //日志左部分：等级和时间
        char leftbuffer[SIZE];
        time_t t = time(nullptr); //获取时间戳
        struct tm* stm = localtime(&t); //获取时间结构
        snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d:%d]", LevelToString(level).c_str(), 
        stm->tm_year+1900, stm->tm_mon+1, stm->tm_mday, stm->tm_hour, stm->tm_min, stm->tm_sec);

        //日志右部分：用户自定义部分
        char rightbuffer[SIZE];
        va_list s;
        va_start(s, format); //操作可变参数
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, s);
        va_end(s);

        //两部分合并
        char logtxt[SIZE*2];
        snprintf(logtxt, sizeof(logtxt), "%s %s\n", leftbuffer, rightbuffer);

        //打印日志
        PrintLog(level, logtxt);
    }

private:
    int _printmethod; //打印去向
    std::string _path;
};

Log lg;
